package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import autoteam.tables.AutoTeamTableModel;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class TeamEditor extends AutoTeamDialog
{
	private static final long serialVersionUID = 3212917466142423204L;

	private JButton changeButton = new JButton("Change");
	private JButton closeButton = new JButton("Cancel");

	String[] DBcoaches =
	{ "Urban Meyer", "Dr. Carter", "Bernie Macen", "BAMF" };

	private String origName;
	private int origCoach;
	private String origSport;
	private String origLeague;

	private JComboBox coachField;
	private JTextField teamField;
	private JTextField sportField;
	private JTextField leagueField;
	
	private AutoTeamTableModel model;
	private int row;
	
	public TeamEditor(JFrame dialogOwner)
	{
		super(dialogOwner);

		this.setTitle("Add New Team");
		this.setDialogSize(new Dimension(300, 175));

		JPanel buttonPane = new JPanel();
		buttonPane.add(changeButton);
		buttonPane.add(closeButton);
		setButtonActions(this);

		JLabel teamName = new JLabel("Team Name:");
		JLabel teamCoach = new JLabel("Team Coach:");
		JLabel sportType = new JLabel("Sport Type:");
		JLabel leagueName = new JLabel("League:");

		// TODO change DBcoaches to a String[] of coaches populated from [RSDB]
		JPanel formPane = new JPanel(new GridLayout(4, 2));
		coachField = new JComboBox(DBcoaches);
		teamField = new JTextField(25);
		sportField = new JTextField(25);
		leagueField = new JTextField(25);

		// TODO Use enriqueDBmagic to make these textFields autoPopulate
		// leagueField.setText( "dataFromTheDB" );
		// sportField.setText( "dataFromTheDB" );
		
		sportField.setEditable(false);
		leagueField.setEditable(false);

		formPane.add(teamName);
		formPane.add(teamField);

		formPane.add(teamCoach);
		formPane.add(coachField);

		formPane.add(sportType);
		formPane.add(sportField);

		formPane.add(leagueName);
		formPane.add(leagueField);

		Container contentPane = getContentPane();
		contentPane.add(formPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.SOUTH);
		pack();
	}
	
	public TeamEditor(JFrame dialogOwner, AutoTeamTableModel model, int row)
	{
		this(dialogOwner);
		this.model = model;
		this.row = row;
		
		origName = (String) model.getValueAt(row, 0);
		origCoach = (Integer) model.getValueAt(row, 1);
		teamField.setText((String) model.getValueAt(row, 0));
		
		DataAccessObject dao = new DataAccessObject();
		List<DBRecord> coachRecords = dao.getAllRecords("coaches");
		for (DBRecord rec : coachRecords)
		{
			coachField.addItem(rec.getFieldValue("firstname") +" "+ rec.getFieldValue("lastname"));
		}
		coachField.setSelectedIndex(origCoach);
		
		
		
	}

	@Override
	protected void setButtonActions(final AutoTeamDialog thisDialog)
	{
		changeButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO change the currently selected league then close the
				// dialog
				// changeLeague( Enrique DBmagic );
				DataAccessObject dao = new DataAccessObject();
				ColumnData[] identifiers = { new ColumnData("teamName",origName) };
				ColumnData[] changes = { new ColumnData("teamName",teamField.getText()) };
				dao.updateRecord("teams", identifiers, changes);
				thisDialog.close();
			}
		});

		closeButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				thisDialog.close();
			}
		});
	}
}
